package com.springboot.bigevent.service.Impl;

import com.springboot.bigevent.mapper.CategoryMapper;
import com.springboot.bigevent.pojo.Category;
import com.springboot.bigevent.service.CategoryService;
import com.springboot.bigevent.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static java.time.LocalDateTime.now;

@Service
public class CategoryControllerImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        //补充属性值
        category.setCreateTime(now());
        category.setUpdateTime(now());
        Map<String,Object> map = ThreadLocalUtil.get();
        category.setCreateUser((Integer) map.get("id"));

        categoryMapper.add(category);
    }

    @Override
    public List<Category> list() {
        //只能查询当前用户创建的分类
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.list(userId);
    }

    @Override
    public Category findById(Integer id) {
        Category c = categoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(now());
        categoryMapper.update(category);
    }


}
